<?php


namespace app\admin\controller\merchant;


use app\admin\controller\com\Admin;
use app\common\controller\AppCommon;
use app\service\MerchantCredits;
use app\service\MerchantMsg;
use app\service\Msg;
use app\service\Page;
use app\service\Uploader;
use think\Db;

class Withdraw extends Admin
{
    public function index()
    {
        $where = [
            'uid_type' => 2
        ];
        $orderBy = 'id desc';
        $page = !empty($this->param['page']) ? intval($this->param['page']) : 1;

        $pageSize = 10;
        $status = !empty($this->param['status']) ? intval($this->param['status']) : '';
        $keyword = !empty($this->param['keyword']) ? $this->param['keyword'] : '';
        if ($status !== '') {
            $where['status'] = $status;
        }
        if (!empty($keyword)) {
            $where['order_no|to_account|username'] = ['like', '%' . $keyword . '%'];
        }

        $total = AppCommon::data_count('withdraw_log', $where);
        $data = AppCommon::data_list('withdraw_log', $where, $page . ',' . $pageSize, '*', $orderBy);
        if ($total) {
            $merchants = AppCommon::db('plugin_merchant')
                ->where(['uid' => ['in', array_column($data, 'uid')]])
                ->column('user_name,account,uid', 'uid');
            foreach ($data as &$value) {
                $value['status_desc'] = '<span class="bs-blue">待审核</span>';
                if ($value['status'] == 1) {
                    $value['status_desc'] = '<span class="bs-green">已通过</span>';
                } elseif ($value['status'] == -1) {
                    $value['status_desc'] = '<span class="bs-red">已驳回</span>';
                }
                if ($value['to_type'] == 1) {
                    $value['to_type_desc'] = '支付宝';
                } elseif ($value['to_type'] == 2) {
                    $value['to_type_desc'] = '微信';
                } else {
                    $value['to_type_desc'] = '银行卡';
                }
                $value['add_time'] = date('Y-m-d H:i:s', $value['add_time']);
                $value['up_time'] = date('Y-m-d H:i:s', $value['up_time']);
                $value['merchant'] = null;
                if (isset($merchants[$value['uid']])) {
                    $value['merchant'] = $merchants[$value['uid']];
                }
            }
            unset($value);
        }

        $this->assign('page', Page::set($data, $pageSize, $page, $total, $this->param, url()));
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function withdraw_action()
    {
        if (!IS_AJAX) {
            data_return('非法操作', -1);
        }

        if (empty($this->param['order_sn'])) {
            data_return('参数有误', -1);
        }
        $orderNo = $this->param['order_sn'];
        $log = AppCommon::data_get('withdraw_log', ['order_no' => $orderNo]);
        if (empty($log)) {
            data_return('记录不存在', -1);
        } elseif ($log['status'] <> 0) {
            data_return('仅限操作待审核记录', -1);
        }
        $status = intval($this->param['astatus']);
        if (!in_array($status, [1, -1])) {
            data_return('不支持的处理方式', -1);
        }
        $reason = $this->param['fail_text'];
        if ($status == -1 && empty($reason)) {
            data_return('请填写驳回理由', -1);
        }
        $upData = [
            'fail_text' => $reason,
            'status' => $status,
            'up_time' => time(),
        ];
        if ($status == 1) {
            if (!empty($_FILES['img'])) {
                $toPath = config('upload')['defaultPath'] . 'withdraw/img/';
                //拼接全连接
                $returnUrl = str_replace(PUBLIC_PATH, URL_WEB, $toPath);
                $res = Uploader::start_upload('img', $toPath, $returnUrl, '', 1);
                if ($res['code'] == 0) {
                    parent::add_admin_log(['title' => '上传文件', 'content' => $res['data']]);
                    $upData['imgs'] = $res['data'];
                } else {
                    data_return($res['msg'], $res['code']);
                }
            }

            $res = AppCommon::data_update('withdraw_log', ['id' => $log['id']], $upData);
            if (!$res) {
                data_return('操作失败，请稍后重试', -1);
            }
            MerchantMsg::add($log['uid'], [
                'title' => '提现申请已通过审核',
                'content' => '您的提现申请已通过审核，预计24小时内到账，请及时查看到账情况，如有疑问请联系客服。',
                'type' => 1
            ]);
            data_return('操作成功，状态已修改');
        } else {
            Db::startTrans();
            $res = AppCommon::data_update('withdraw_log', ['id' => $log['id']], $upData);
            if (!$res) {
                Db::rollback();
                data_return('操作失败', -1);
            }
            //申请金额+手续费
            $num = $log['num'] + $log['fee'];
            $back = MerchantCredits::set($log['uid'], 'credit', $num, ['remark' => '提现申请被驳回', 'type' => 1]);

            if (!$back) {
                Db::rollback();
                data_return('余额归还失败，请稍后重试', -1);
            }
            Db::commit();
            MerchantMsg::add($log['uid'], [
                'title' => '提现申请未通过，请登录账号查看原因',
                'content' => '抱歉，您申请的提现未通过审核，请及时查看原因后重新提交申请，如有疑问请联系客服。',
                'type' => 1
            ]);
            data_return('驳回成功');
        }
    }
}